﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi_Gui.LayoutManager;
using Yugi.Client.ViewModel;
using Yugi.Client;
namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for LoginLayout.xaml
	/// </summary>
	public partial class LoginLayout : UserControl , IInvoke
	{
        MainServerHandler mainserver;
        LoginModel model;
		public LoginLayout()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(LoginLayout_Loaded);
            login_control.user_name_txt.Focus();
		}

        void LoginLayout_Loaded(object sender, RoutedEventArgs e)
        {
            mainserver = ((Window)(this.Parent)).DataContext as MainServerHandler;
            model = mainserver.Login_Model;
            model.Presenter = this;
            this.login_control.OnLoginOk += new Action<string,string>(login_control_OnLoginOk);
        }

        void login_control_OnLoginOk(string us,string pass)
        {
            if(us=="1" && pass=="1")
            {
                LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutManager.LayoutType.Externa);
            }
            else
            {
                mainserver.SendMessageToServer(Common.MSG_GSM.L_Login,us,pass);
                
            }
        }

        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch(msg)
            {
                case ClientMSG.Login_OK:
                    model.userlogin.SetUserName(this.login_control.user_name_txt.Text);
                    model.userlogin.SetPassword(this.login_control.pass_txt.Password);
                    model.boxuserlogin.Add(model.userlogin);
                    model.boxuserlogin.Save();
                    LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutManager.LayoutType.Yugiohvietnam);
                    mainserver.External_Model.UserID = Convert.ToInt32(obj[0]);
                    break;
                case ClientMSG.Login_Error:
                    MessageBox.Show("Login Fail");
                    break;
                case ClientMSG.Update_Friend_OK:
                    try
                    {
                        model.getfriend(Convert.ToInt32(obj[0])).IsOnline = 1;
                    }
                    catch { }
                    break;
                case ClientMSG.Update_Friend_Off_OK:
                    try
                    {
                        model.getfriend(Convert.ToInt32(obj[0])).IsOnline = 0;
                    }
                    catch { }
                    break;
            }

        }

        public void Refresh()
        {

        }
        #endregion

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Canvas.SetLeft(login_control, ( e.NewSize.Width-login_control.Width)/2);
            Canvas.SetTop(login_control, (e.NewSize.Height - login_control.Height) / 2);
        }

        private void Link_Web_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("firefox.exe", "www.google.com");
            }
            catch
            {
                System.Diagnostics.Process.Start("www.google.com");
            }
        }
    }
}